//
//  AuthenCompanySettleMsgVM.swift
//  kxf
//
//  Created by Mac on 2024/3/8.
//

import Foundation
import UIKit
import SwiftUI

class AuthenCompanySettleMsgVM: UIView {
    
    var selfHeight = RegisterViewHeadTitleVM().selfHeight+RegisterInputVM().selfHeight*6
    
    var controller = WHBaseViewVC()
    var typeArr = [["name":"对私账户","type":"2"],
                   ["name":"对公账户","type":"1"]]
    var legalTypeArr = [["name":"法人","type":"LEGAL_PERSON"],
                   ["name":"非法人","type":"UNINCORPORATE"]]
//    var bankTypeDict = NSDictionary()
//    var legalTypeDict = NSDictionary()
    
    var bankTypeChangeBlock:((NSDictionary)->())?
    var settleTypeChangeBlock:((NSDictionary)->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
        
        if AuthenCompanyMsgModel.shareInstance.settleWay == ""{
            let bankTypeDict = typeArr[0] as NSDictionary
            self.bankTypeVm.setLabelContent(content: "\(bankTypeDict["name"]as? String ?? "")")
            let legalTypeDict = legalTypeArr[0] as NSDictionary
            self.legalTypeVm.setLabelContent(content: "\(legalTypeDict["name"]as? String ?? "")")
            
            AuthenCompanyMsgModel.shareInstance.settleBankType =
            bankTypeDict["type"]as? String ?? ""
            AuthenCompanyMsgModel.shareInstance.settleBankTypeName =
            bankTypeDict["name"]as? String ?? ""
            
            AuthenCompanyMsgModel.shareInstance.settleWay =
            legalTypeDict["type"]as? String ?? ""
            AuthenCompanyMsgModel.shareInstance.settleWayName =
            legalTypeDict["name"]as? String ?? ""
        }
        updateAccountType()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var titleVm : RegisterViewHeadTitleVM = {
        let vm = RegisterViewHeadTitleVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.titleLabel.text = "结算信息"
        return vm
    }()
    lazy var idNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "持卡人"
        vm.textField.placeholder = "请输入持卡人姓名"
        vm.textField.keyboardType = .default
//        vm.maxLength = 10
        
        return vm
    }()
    lazy var legalTypeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.titleVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "结算对象"
        vm.textField.placeholder = "法人结算"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.showLegalTypeAlert()
        }
        return vm
    }()
    lazy var bankTypeVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.legalTypeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "结算账户类型"
        vm.textField.placeholder = "请选择结算账户类型"
        vm.showArrowImg()
        vm.tapBlock = {()in
            self.showTypeAlert()
        }
        return vm
    }()
    lazy var bankNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankTypeVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "银行卡号"
        vm.textField.placeholder = "请输入银行卡号"
//        vm.textField.keyboardType = .default
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 30
        return vm
    }()
    
    lazy var bankPhoneVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "预留手机号"
        vm.textField.placeholder = "请输入预留手机号"
        vm.textField.keyboardType = .numberPad
        vm.maxLength = 11
        return vm
    }()
    lazy var bankPrivateNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPhoneVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对私银行名称"
        vm.textField.placeholder = "请填写银行名称"
        vm.isHidden = true
        vm.textField.keyboardType = .default
        return vm
    }()
    lazy var bankPrivateBranchVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPrivateNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对私开户支行"
        vm.isHidden = true
        vm.textField.placeholder = "请选择开户支行"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenBankBranchListVC()
            vc.tapBlock = {(dict)in
                AuthenCompanyMsgModel.shareInstance.settleBankBranchCodePrivate = "\(dict["branchNo"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate = "\(dict["branchBankName"]as? String ?? "")"
                self.bankPrivateBranchVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate)
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var bankPublicNoVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPhoneVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对公银行账号"
        vm.textField.placeholder = "请输入对公银行账号"
        vm.textField.keyboardType = .numberPad
        return vm
    }()
    lazy var bankPublicNameVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPublicNoVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对公银行名"
        vm.textField.placeholder = "请输入对公银行名"
        vm.textField.keyboardType = .default
        return vm
    }()
    lazy var bankPublicBranchVm : RegisterInputVM = {
        let vm = RegisterInputVM.init(frame: CGRect.init(x: 0, y: self.bankPublicNameVm.frame.maxY, width: 0, height: 0))
        vm.leftLabel.text = "对公银行支行名"
        vm.textField.placeholder = "请选择对公银行支行名"
        vm.showArrowImg()
        vm.tapBlock = {()in
            let vc = AuthenBankBranchListVC()
            vc.tapBlock = {(dict)in 
                AuthenCompanyMsgModel.shareInstance.settleBankBranchCodePublic = "\(dict["branchNo"]as? String ?? "")"
                AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePublic = "\(dict["branchBankName"]as? String ?? "")"
                self.bankPublicBranchVm.setLabelContent(content: AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePublic)
            }
            self.controller.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
}

extension AuthenCompanySettleMsgVM{
    func initUI() {
        addSubview(titleVm)
//        addSubview(idNameVm)
        addSubview(bankNoVm)
        addSubview(legalTypeVm)
        addSubview(bankTypeVm)
        addSubview(bankPrivateNameVm)
        addSubview(bankPrivateBranchVm)
        addSubview(bankPhoneVm)
        addSubview(bankPublicNoVm)
        addSubview(bankPublicNameVm)
        addSubview(bankPublicBranchVm)
        
    }
    func updateUIForRefuse() {
        bankNoVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNo
        bankTypeVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankTypeName
        legalTypeVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleWayName
        bankPrivateNameVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate
        bankPrivateBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate)")
        bankPhoneVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankPhone
        bankPublicNoVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNoPublic
        bankPublicNameVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNamePublic
        bankPublicBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePublic)")
    }
    //MARK: 企业，富友通道，对私，法人结算的时候，需要传对公对私所有的信息
    func updateUIForFYPublic() {
        //只有企业进件的富友通道，才进行处理
        
        if UserModel.shared.posChannel == "fy" && AuthenCompanyMsgModel.shareInstance.merchantType == "0"{
            //非法人结算，只需要银行卡卡号照片
            if AuthenCompanyMsgModel.shareInstance.settleWay == "UNINCORPORATE"{
                
            }else{
                if AuthenCompanyMsgModel.shareInstance.settleBankType == "2"{
                    //富友通道，企业，对私，法人结算的时候，需要传对公对私所有的信息
                    
                    return
                }else{
                    
                }
            }
            if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
            }
        }else{
            if AuthenCompanyMsgModel.shareInstance.settleBankType == "1"{
                
            }else{
                
            }
        }
    }
    func showLegalTypeAlert(){
        let alert = UIAlertController(title: "请选择结算对象", message: nil, preferredStyle: .actionSheet)
        
        for i in 0..<legalTypeArr.count{
            let dict = legalTypeArr[i]as NSDictionary
            let action = UIAlertAction(title: "\(dict["name"]as? String ?? "")", style: .default) { action in
//                self.legalTypeDict = dict
                AuthenCompanyMsgModel.shareInstance.settleWay =
                dict["type"]as? String ?? ""
                AuthenCompanyMsgModel.shareInstance.settleWayName =
                dict["name"]as? String ?? ""
                self.legalTypeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleWayName)")
                
                self.updateAccountType()
                
                if self.settleTypeChangeBlock != nil{
                    self.settleTypeChangeBlock!(dict)
                }
            }
            alert.addAction(action)
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel)
        alert.addAction(cancelAction)
        self.controller.present(alert, animated: true)
    }
    func showTypeAlert(){
        let alert = UIAlertController(title: "请选择结算账户类型", message: nil, preferredStyle: .actionSheet)
        
        for i in 0..<typeArr.count{
            let dict = typeArr[i]as NSDictionary
            let action = UIAlertAction(title: "\(dict["name"]as? String ?? "")", style: .default) { action in
//                self.bankTypeDict = dict
                AuthenCompanyMsgModel.shareInstance.settleBankType =
                dict["type"]as? String ?? ""
                AuthenCompanyMsgModel.shareInstance.settleBankTypeName =
                dict["name"]as? String ?? ""
                self.bankTypeVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankTypeName)")
                
                self.updateAccountType()
                
                if self.bankTypeChangeBlock != nil{
                    self.bankTypeChangeBlock!(dict)
                }
            }
            alert.addAction(action)
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel)
        alert.addAction(cancelAction)
        self.controller.present(alert, animated: true)
    }
    func updateAccountType(){
        if AuthenCompanyMsgModel.shareInstance.settleBankType == "1"{//对公
            self.bankNoVm.isHidden = true
            self.bankPublicNoVm.isHidden = false
            self.bankPublicNameVm.isHidden = false
            self.bankPublicBranchVm.isHidden = false
            self.bankPrivateNameVm.isHidden = true
            self.bankPrivateBranchVm.isHidden = true
            
            if AuthenCompanyMsgModel.shareInstance.settleWay == "UNINCORPORATE"{
                self.bankPhoneVm.isHidden = true
                self.bankPublicNoVm.isHidden = true
                self.bankPublicNameVm.isHidden = true
                self.bankPublicBranchVm.isHidden = true
//                self.bankPublicNameVm.frame = self.bankPhoneVm.frame
//                self.bankPublicBranchVm.frame = self.bankPublicNoVm.frame
            }else{
                self.bankPhoneVm.isHidden = true
//                self.bankPublicNoVm.isHidden = false
                self.bankPublicNoVm.frame = self.bankNoVm.frame
                self.bankPublicNameVm.frame = CGRect.init(x: 0, y: self.bankPublicNoVm.frame.maxY, width: self.bankPublicNoVm.frame.size.width, height: self.bankPublicNoVm.frame.size.height)
                self.bankPublicBranchVm.frame = CGRect.init(x: 0, y: self.bankPublicNameVm.frame.maxY, width: self.bankPublicNoVm.frame.size.width, height: self.bankPublicNameVm.frame.size.height)
            }
        }else{//对私
            if AuthenCompanyMsgModel.shareInstance.settleWay == "UNINCORPORATE"{
                self.bankPublicNoVm.isHidden = true
                self.bankPublicNameVm.isHidden = true
                self.bankPublicBranchVm.isHidden = true
                self.bankPrivateNameVm.isHidden = true
                self.bankPrivateBranchVm.isHidden = true
                self.bankPhoneVm.isHidden = true
                self.bankNoVm.isHidden = false
            }else{
                self.bankNoVm.isHidden = false
                self.bankPublicNoVm.isHidden = true
                self.bankPublicNameVm.isHidden = true
                self.bankPublicBranchVm.isHidden = true
                self.bankPrivateNameVm.isHidden = false
                self.bankPrivateBranchVm.isHidden = false
                self.bankPhoneVm.isHidden = false
            }
//            self.bankPublicNoVm.isHidden = true
//            self.bankPublicNameVm.isHidden = true
//            self.bankPublicBranchVm.isHidden = true
//            self.bankPrivateNameVm.isHidden = false
//            self.bankPrivateBranchVm.isHidden = false
//            self.bankPhoneVm.isHidden = false
            
//            self.bankPublicNameVm.frame = CGRect.init(x: 0, y: self.bankPublicNoVm.frame.maxY, width: self.bankPublicNoVm.frame.size.width, height: self.bankPublicNoVm.frame.size.height)
//            self.bankPublicBranchVm.frame = CGRect.init(x: 0, y: self.bankPublicNameVm.frame.maxY, width: self.bankPublicNoVm.frame.size.width, height: self.bankPublicNameVm.frame.size.height)
        }
    }
}

